<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: !filterInfo.selectBrandId }"
          @click="filterInfo.selectBrandId = ''"
          >全部</a
        >
        <a
          href="javascript:;"
          :class="{ active: filterInfo.selectBrandId === i.id }"
          @click="filterInfo.selectBrandId = i.id"
          v-for="i in filterInfo.brands"
          :key="i.id"
          >{{ i.name }}</a
        >
      </div>
    </div>
    <!-- 属性 -->
    <div class="item" v-for="item in filterInfo.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: !item.selectPropertiesId }"
          @click="item.selectPropertiesId = ''"
          >全部</a
        >
        <a
          @click="item.selectPropertiesId = i.id"
          href="javascript:;"
          :class="{ active: item.selectPropertiesId === i.id }"
          v-for="i in item.properties"
          :key="i.id"
          >{{ i.name }}</a
        >
      </div>
    </div>
  </div>
</template>
<script>
import { categorySubFilter } from '@/api/category'
import { onBeforeRouteLeave, useRoute } from 'vue-router'
import { ref, watch } from 'vue'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const route = useRoute()
    const filterInfo = ref({})
    const stopFilter = watch(
      () => route.params.id,
      () => {
        // 获取当前筛选数据
        categorySubFilter(route.params.id).then(res => {
          filterInfo.value = res.result
          // console.log('筛选数据:', res)
        })
      },
      {
        immediate: true
      }
    )
    // 通过侦听filterInfo值的改变触发父组件方法
    const stopInfo = watch(
      () => filterInfo.value,
      newVal => {
        const temp = {
          brandId: filterInfo.value.selectBrandId, // 品牌id
          attrs: filterInfo.value.saleProperties.map(item => {
            //   找出当前选中项对应的属性名字
            const findTemp = item.properties.find(item2 => {
              return item2.id === item.selectPropertiesId
            })
            return {
              groupName: item.name,
              propertyName: findTemp ? findTemp.name : ''
            }
          })
        }
        emit('change', temp)
      },
      { deep: true }
    )
    // onBeforeRouteEnter(() => {
    //   console.log('onBeforeRouteEnter')
    // })

    onBeforeRouteLeave(() => {
      stopFilter()
      stopInfo()
    })
    return { filterInfo }
  }
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
